<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<!DOCTYPE>
<html>

<head lang="en">
    <meta charset="UTF-8">
    <title>有间花店 | 后台管理</title>
    <link rel="shortcut icon" type="image/x-icon" href="img/favicon.ico">
    <link href="css/bootstrap.min.css" rel="stylesheet">

    <script src="js/html5shiv.js"></script>
    <script src="js/respond.min.js"></script>
    <!-- 引入css样式-->
    <link rel="stylesheet" type="text/css" href="css/admin.css"
          media="screen">
    <script src="js/jquery-1.11.3.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script src="js/admin.js"></script>

    <style type="text/css">
        a:focus, a:hover {
            text-decoration: none;
        }

        .panel-body {
            text-align: left;
        }

        textarea {
            resize: none;
        }

        #mytable th, #mytable td {
            text-align: center;
            vertical-align: middle;
            font-size: 14px;
        }
        /*点击修改出现提示框*/
        .click {
            margin-top: -320px;
            margin-left: 80px;
            padding: 30px;
            float: left;

        }

        .modal-open .modal {
            overflow-y: hidden;
            width: 620px;
            height: 440px;
            margin-top: 90px;
            margin-left: 440px;
            border-radius: 10%;
            border: 1px solid #cccccc;
            background: #CAE8EA;
            display: none;
        }

        .photo img {
            width: 70px;
            height: 70px;
            border: transparent;
            text-align: center;
        }
    </style>
</head>

<body>

<div class="wrap">
    <!-- 左边内容 -->
    <jsp:include page="/admin_menu.jsp"></jsp:include>

    <!-- 右边内容 -->
    <div id="right" class="tab-content right">

        <!-- 模块一商品查询-->
        <div>
            <!-- 商品查询，修改，删除表格，bootstrap自适应表格-->
            <div class="table-responsive">
                <!-- 点击修改出现提示框-->
               <%-- <div class="modal fade" id="editProduct" tabindex="-1"
                     role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">--%>
                    <form id="sp_add_left1" style="padding-top: 10px;" >
                        <ul>
                            <li><label>商品编号：</label> <input type="text" name="pid" value="${product.pid}"
                                                            id="pid" style="border: 0; background-color: #CAE8EA;" disabled>
                            </li>
                            <li><label>商品名称：</label> <input type="text" name="pname" value="${product.pname}" onblur="check()"
                                                            id="pname"></li>
                            <li><label>上市时间：</label> <input type="date" name="pdate" value="${product.pdate}" onblur="check()"
                                                            id="pdate"></li>
                            <li><label>商品类型：</label> <select id="select" name="cid" onblur="check()">
                                <option value="">请选择</option>
                                <c:forEach items="${categories}" var="categories">
                                    <option name="${categories.cid}" value="${categories.cid}">${categories.cname}</option>
                                </c:forEach>
                            </select> </li>
                            <li><label>鲜花素材：</label> <input type="text"
                                                            name="pmaterial" id="pmaterial" value="${product.pmaterial}" onblur="check()"></li>
                            <li><label>库存量 &nbsp;&nbsp;&nbsp;：</label> <input
                                    type="text" name="num" id="num" value="${product.num}" onblur="check()"></li>
                            <li><label>&nbsp;市场价 &nbsp;&nbsp;：</label> <input
                                    type="text" name="market_price" id="market_price" value="${product.market_price}" onblur="check()"></li>
                            <li><label>&nbsp;&nbsp;&nbsp;定价 &nbsp;&nbsp;&nbsp;：</label>
                                <input type="text" name="shop_price" id="shop_price" value="${product.shop_price}" onblur="check()"></li>
                        </ul>
                        <div id="sp_add_right"
                             style="top: -374px; right: -328px; float: none">
                            <ul style="margin-left: -26px;">
                                <li><label>花语&nbsp;&nbsp;&nbsp;：</label> <textarea
                                        id="pdesc" style="margin-top: 0px; top: 16px;" name="pdesc" type="text" value="${product.pdesc}" onblur="check()">${product.pdesc}</textarea>
                                </li>
                                <li style="margin: 0 0 20px -16px;"><label>是否上架：</label> <select onblur="check()"
                                        style="height: 32px; width: 174px;" name="pflag" id="pflag">
                                    <option name="pflago1" value="0">否</option>
                                    <option name="pflago2" value="1">是</option>
                                </select></li>
                                <li style="margin: 0 0 20px -16px;"><label>是否特价：</label> <select onblur="check()"
                                        style="height: 32px; width: 174px;" name="pbargain"
                                        id="pbargain">
                                    <option value="0" name="pbargain1">否</option>
                                    <option value="1" name="pbargain2">是</option>
                                </select></li>
                                <li style="margin: 0 0 20px -16px;"><label>是否热门：</label> <select onblur="check()"
                                        style="height: 32px; width: 174px;" name="is_hot" id="is_hot" >
                                    <option value="0" name="is_hot1">否</option>
                                    <option value="1" name="is_hot2">是</option>
                                </select></li>
                                <li><label>图片 &nbsp;&nbsp;：</label> <label for="files">
                                    <img src="${product.pimage}"  id="oImg" width="70px" height="70px"> <br /> <input
                                        type="text" id="pimage" name="pimage" style="display: none" value="${product.pimage}" onblur="check()" >
                                </label> <input type="file" id="files" onchange="inputImg();">
                                </li>
                            </ul>
                        </div>
                    </form>
                    <div class="click">
                        <button type="button" class="btn btn-primary"
                                data-dismiss="modal" onclick="guanbi()" >关闭</button>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        <button type="button" class="btn btn btn-info"
                                data-toggle="button" onclick="updateProduct()" id="tijiao" disabled>保存修改</button>
                    </div>
                <%--</div>--%>
            </div>
            <script>



                if (${product.pflag}){
                    $("option[name='pflago2']").attr("selected","selected")
                }else {
                    $("option[name='pflago1']").attr("selected","selected")
                }
                if (${product.pbargain}){
                    $("option[name='pbargain2']").attr("selected","selected")
                }else {
                    $("option[name='pbargain1']").attr("selected","selected")
                }
                if (${product.is_hot}){
                    $("option[name='is_hot2']").attr("selected","selected")
                }else {
                    $("option[name='is_hot1']").attr("selected","selected")
                }


                <c:forEach items="${categories}" var="categories">
                if ($("option[name='${categories.cid}']").val()==${product.cid}){
                    $("option[name='${categories.cid}']").attr("selected","selected");
                }
                </c:forEach>



               var btn= document.getElementById("tijiao");



                function guanbi() {//取消修改，回到首页信息
                    location.href="/selectAllProduct.do";//取消修改完后，查看所有商品
                };

                function check() {
                var tijiao=$("#tijiao");
                var pname=document.getElementById("pname").value;
                var pdate=document.getElementById("pdate").value;
                var pmaterial=document.getElementById("pmaterial").value;
                    var num=document.getElementById("num").value;
                    var market_price=document.getElementById("market_price").value;
                    var shop_price=document.getElementById("shop_price").value;
                    var pdesc=document.getElementById("pdesc").value;
                    var select=$("#select").val();
                   // alert(pdesc);


                    if (pname!=null && pname!='' && pdate!=null && pdate!='' && pmaterial!=null && pmaterial!=''
                    && num!=null && num!=''&&parseInt(market_price)>=0 && market_price!=null && market_price!=''&&parseInt(market_price)>0 &&parseInt(market_price) >= parseInt(shop_price) && shop_price!=null && shop_price!='' && parseInt(shop_price)>0&&select!=""){
                        tijiao.removeAttr("disabled");
                        return true;
                    }else {
                        if (num<0){
                            alert("库存不能小于0");
                            return false;
                        }
                        if (parseInt(market_price)<1){
                            alert("市场价格不能小于0")
                            return false;

                        }
                        if (parseInt(shop_price)<1){
                            alert("销售价格不能小于0")
                            return false;
                        }
                        if (select==""){
                            alert("请选择分类");
                            return false;
                        }
                        if (parseInt(market_price) < parseInt(shop_price)){
                            return alert("销售价不能大于市场价");
                        }

                        tijiao.attr("disabled","disabled");
                        return false;
                    }
                };
                function updateProduct(){
                    //alert($("#sp_add_left1").serialize())
                    var url = "${pageContext.request.contextPath}/updateProduct.do?pid=" + $("#pid").val() ;
                    $.post(url, $("#sp_add_left1").serialize(), function(data){
                        if(data == "OK"){
                            alert("商品修改成功！");
                             location.href="/selectAllProduct.do";//修改完后，查看所有商品
                        }else{
                            alert("商品修改失败！");
                            window.location.reload();//刷新当前页面
                        }
                    })
                };

                function inputImg() {
                    var input = document.getElementById('files');
                    var url = window.URL.createObjectURL(input.files[0]);
                    var name = input.files[0].name;
                    //动态赋予图片路径
                    $('#oImg').attr("src", url);
                    $('#pimage').val("product/" + name);
                }
            </script>
        </div>
    </div>
</div>

</body>

</html>